<?php

    // Управляющие инструкции
    $mikron->OperatingInstructions->AddFunction("Presentation", "Presentation");
    $mikron->OperatingInstructions->AddFunction('MENU', "getMenu");
    $mikron->OperatingInstructions->AddFunction('GUESTFORM', "GuestForm");
    $mikron->OperatingInstructions->AddFunction('BOTTOM', "BottomBlock");
    $mikron->OperatingInstructions->AddFunction('TECHSUPPORT_FORM', "TechForm");

    /*function getLeftMenu($cat = 4)
    {
        global $mikron;
        $cat = (int)$cat;
        $items = $mikron->EntityManager->Query("T_MENU", null);
        $curpage = $mikron->Page->getCurrentPage();
        $pos = strpos($curpage, "?");
        if($pos!==false)
        {
            $curpage2 = substr($curpage, 0, $pos);
        }
        $parentmenu = 0;
        foreach($items as $item)
        {
            $url = $item->link->value;
            if($curpage == $url || $curpage2 == $url)
            {
                $parentmenu = $item->id;
                break;
            }
        }
        reset($items);
        foreach($items as $item)
        {
            $url = $item->link->value;
            if(!is_null($item->parentmenu->value))
            {
                if($curpage == $item->link->value)
                {
                    $parentmenu = $item->parentmenu->value->id;
                    break;
                }
            }
        }
        $items = $mikron->EntityManager->Query("T_MENU", null, "`menugroup` = $cat AND `parentmenu` = $parentmenu");
        $tbl = new html_table();
        
        foreach($items as $item)
        {
            $title = $item->title->value;
            $url = $item->toURL();
            if($curpage == $url)
            {
                $tbl->newcol(null, true, false, null, "leftselectedlink");
                ?><?php echo $title;?> &rarr;<?php
            }
            else
            {
                $tbl->newcol(null, true, false, null, "leftlink");
                ?><a href="<?php echo $url;?>"><?php echo $title;?></a><?php
            }
        }
        $tbl->end();
    }*/

    // FAQ
    function getFAQ()
    {
        echo "<table cellspacing=0 cellpadding=0><tr><td>";
        echo "{{nicewindow style=vitacard caption=\"Вопросы-ответы\"}}";
        global $mikron;
        $tbl = new html_table(2, null, "20%", 0, 8);
        $items = $mikron->EntityManager->Query("T_FAQ");
        foreach($items as $item)
        {
            $tbl->newcol("<img src=\"/mikron/sites/vitacard/images/apply.png\">");
            $tbl->newcol($item->toString(), true, false, null, "valigntop", null, "middle");
        }
        $tbl->end();
        echo "{{/nicewindow}}";
        echo "</td></tr></table>";
    }

    function getNews()
    {
        global $mikron;
        echo "<h1>Новости:</h1>";
        $tbl = new html_table(1, null, null, 0, 8);
        $items = $mikron->EntityManager->Query("T_NEWS", null);
        foreach($items as $item)
        {
            $tbl->newcol($item->toString());
        }
        $tbl->end();
    }

    function getPartners()
    {
        global $mikron;
        echo "<h1>Наши партнеры:</h1>";
        $tbl = new html_table(2, null, null, 0, 0);
        $regforms = $mikron->EntityManager->Query("T_REGFORM", null, "`regstate` = 1 AND `exstatus` >= 1");
        $cnt = 1;
        foreach($regforms as $regform)
        {
            $tbl->newcol($regform->toString());
            $cnt++;
        }
        $tbl->end();
    }

    function getDistributors()
    {
        global $mikron;
        $tbl = new html_table(3, null, null, 0, 8);
        $regforms = $mikron->EntityManager->Query("T_REGFORM", null, "`regstate` = 1 AND `exstatus` = 2");
        foreach($regforms as $regform)
        {
            $tbl->newcol($regform->toString());
        }
        $tbl->end();
    }

    function startshadowbox()
    {
        ?>
        <table cellspacing="0" cellpadding="0" class="s_right"><tr><td>
        <table cellspacing="0" cellpadding="0" class="s_bottom"><tr><td>
        <table cellspacing="0" cellpadding="0" class="s_bottomright"><tr><td>
        <table cellspacing="0" cellpadding="0" class="s_topright"><tr><td>
        <table cellspacing="0" cellpadding="0" class="s_bottomleft"><tr><td>
        <table cellspacing="0" cellpadding="0" class="s_box"><tr><td>
        <!-- div class="s_right">
        <div class="s_bottom">
        <div class="s_bottomright">
        <div class="s_topright">
        <div class="s_bottomleft">
        <div class="s_box" -->
        <?php
    }

    function endshadowbox()
    {
        ?>
        </td></tr></table>
        </td></tr></table>
        </td></tr></table>
        </td></tr></table>
        </td></tr></table>
        </td></tr></table>
        <!-- /div></div></div></div></div></div --><?php
    }

    function needlogged()
    {
        global $mikron;
        session_start();
        $logged = null;
        $chance = false;
        if(isset($_SESSION['user']) || (isset($_POST['login']) && isset($_POST['pass'])) )
        {
            $chance = true;
            if(isset($_POST['pass']))
            {
                $enterdata = md5(md5($_POST['pass']).$_POST['login']);
            }
            else
            {
                $enterdata = $_SESSION['user'];
            }
            $users = $mikron->EntityManager->Query("T_USER");
            foreach($users as $user)
            {
                $md5u = md5($user->password->value.$user->login->value);
                if($md5u == $enterdata)
                {
                    $_SESSION['useraccount'] = $user;
                    if(isset($_SESSION['user']))
                    {
                        return true;
                    }
                    $logged = $user->login->value;
                    $_SESSION['user'] = $enterdata;
                    header("Location: ".$_POST['backpage']);
                    exit;
                    break;
                }
            }
        }
        if(is_null($logged))
        {
            if($chance)
            {
                ?>{{$LOGINERROR}}<?php
            }
            ?>
            <p>Для доступа необходимо ввести логин и пароль.</p>
            
            <form action="/updates/" method="post">
            <input type="hidden" name="backpage" value="<?php echo $_SERVER['REQUEST_URI']; ?>">
            <table>
                <tr><td>E-mail:</td><td><input name="login"></td></tr>
                <tr><td>Пароль:</td><td><input name="pass" type="password"></td></tr>
                <tr><td>&nbsp;</td><td><input type=submit value="Войти"></td></tr>
            </table>
            </form>
            {{$PLEASEREG}}
            <?php
            $mikron->Page->EndBody();
            exit;
        }
    }

?>